package com.zmn.oms.task.config;

import com.zmn.oms.common.constant.CdsConsts;
import com.zmn.oms.common.constant.InsConsts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2021/03/29 17:58
 */
@Slf4j
@Component
public class EnvironmentConfig implements EnvironmentAware {

    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        env = environment;

        this.initConsts(environment);
    }

    // 利用配置初始化伪常量
    private void initConsts(Environment env) {
        log.info("=========================== 伪常量初始化 ===========================");
        InsConsts.CPIC_SYNC_KEY = env.getProperty("ins.cpic.sync.key");
        log.info("ins.cpic.sync.key=[{}]", InsConsts.CPIC_SYNC_KEY);
        InsConsts.CPIC_SYNC_URL = env.getProperty("ins.cpic.sync.url");
        log.info("ins.cpic.sync.url=[{}]", InsConsts.CPIC_SYNC_URL);

        InsConsts.CPIC_INSURE_APPID = env.getProperty("ins.cpic.insure.appid");
        log.info("ins.cpic.insure.appid=[{}]", InsConsts.CPIC_INSURE_APPID);
        InsConsts.CPIC_INSURE_SECRETKEY = env.getProperty("ins.cpic.insure.secretKey");
        log.info("ins.cpic.insure.secretKey=[{}]", InsConsts.CPIC_INSURE_SECRETKEY);
        InsConsts.CPIC_INSURE_URL = env.getProperty("ins.cpic.insure.url");
        log.info("ins.cpic.insure.url=[{}]", InsConsts.CPIC_INSURE_URL);
        InsConsts.CPIC_INSURE_CALLBACK_URL = env.getProperty("ins.cpic.insure.callbackUrl");
        log.info("ins.cpic.insure.callbackUrl=[{}]", InsConsts.CPIC_INSURE_CALLBACK_URL);

        String msgRuleId = env.getProperty("ins.cpic.fail.ruleId");
        log.info("ins.cpic.fail.ruleId=[{}]", msgRuleId);
        if (msgRuleId != null && Pattern.matches("\\d+", msgRuleId)) {
            InsConsts.MSG_RULE_ID = Integer.parseInt(msgRuleId);
        }
        InsConsts.DINGTALK_ID_FOR_RECIVE_MSG = env.getProperty("ins.cpic.fail.dingTalkIds");
        log.info("ins.cpic.fail.dingTalkIds=[{}]", InsConsts.DINGTALK_ID_FOR_RECIVE_MSG);
        InsConsts.ZMN_TOPIC_ENGINEER = env.getProperty("spring.mq.topic.engineer");
        log.info("spring.mq.topic.engineer=[{}]", InsConsts.ZMN_TOPIC_ENGINEER);

        {// CDS调用
            CdsConsts.HttpCall.BASE_URL = env.getProperty("cds.api.base-url");
            log.info("cds.api.base-url=[{}]", CdsConsts.HttpCall.BASE_URL);
            CdsConsts.HttpCall.MODEL_NAME = env.getProperty("cds.api.app-name");
            log.info("cds.api.app-name=[{}]", CdsConsts.HttpCall.MODEL_NAME);
            CdsConsts.HttpCall.SECRET_KEY = env.getProperty("cds.api.secretkey");
            log.info("cds.api.secretkey=[{}]", CdsConsts.HttpCall.SECRET_KEY);
        }

        log.info("=========================== 伪常量初始化结束 ===========================");
    }
}
